

//打开增加遗传病史 框
function addHealthHeredityPre(t){
	var memberId=$("#memberId").val();
	var health_heredity_id=$(t).attr("data-id");
	$.ajax({ 
		type : "get",
		dataType: "html",
		url : "../heredity/addHealthHeredityPre",
		async: true,
		data:{
			memberId:memberId,
			healthHeredityId:health_heredity_id
		},
		success : function(data) {
			$("#healthdialog").empty().html(data);
			deleteM(t,null);
		}
	});
}

//增加遗传病史动作
function addHealthHeredity(){
	var memberId=$("#memberId").val();
	var remark=$("#heredityRemark").val();
	var health_heredity_id;
	var remark=$("#heredityRemark").val();
	
	var relation=$("#relation").val();
	var diseaseName=$("#diseaseName").val();
	var sizeOfList=$("#sizeOfListHeredity").val();
	
	
	var hospitalName = $("#hospitalName").val();
	var confirmTime = $("#confirmTime").val();
	
	
	if(!validateLength(diseaseName,"疾病名称",1,16)){
		return;
	}
	if(diseaseName==''){
		zhyAlert("请填写【疾病名称】");
		return;
	}
	if(!interceptIllegal(diseaseName)){
		zhyAlert("【疾病名称】含有非法字符");
		return;
	}
	
	if(hospitalName){
		if($.trim(hospitalName).length > 32){
			zhyAlert("【诊断医院或社区】应少于32个字符！");
			return;
		}
		if(!interceptIllegal(hospitalName)){
			zhyAlert("【诊断医院或社区】含有非法字符");
			return;
		}
	}
	
	if(!validateDate(confirmTime)){
		zhyAlert("时间格式错误！");
		return;
	}

	if(remark){
		if($.trim(remark).length > 120){
			zhyAlert("【备注】应少于120个字符！");
			return;
		}
		if(!interceptIllegal(remark)){
			zhyAlert("【备注】含有非法字符");
			return;
		}
	}
	
	$.ajax({
		type : "post",
		dataType: "json",
		url : "../heredity/addHealthHerediy",
		async: true,
		data:{
			memberId:memberId,
			diseaseName:diseaseName,
			remark:remark,
			confirmTime:confirmTime,
			hospitalName:hospitalName
		},
		success : function(data) {
			if(data.isSuccess){
				var heal_edit1 = data.heal_edit;//修改标示
				var heal_delete1 = data.heal_delete;//删除标示
				health_heredity_id=data.health_heredity_id;
				sizeOfList=(eval(sizeOfList)+1);
				health_heredity_id=data.health_heredity_id;
				addRowHeredity(sizeOfList,diseaseName,health_heredity_id,remark,heal_edit1,heal_delete1,hospitalName,confirmTime);
				hidePop();
				
			}else{
				zhyAlert("保存失败");
				hidePop();
			}
		}
	});
}





//打开修改框
function updateHeredityPre(t){
	$.ajax({ 
		type : "get",
		dataType: "html",
		url : "../heredity/updateHealthHeredityPre",
		async: true,
		data:{
			healthHeredityId:$(t).attr("data-id"),
			sn:$(t).attr("data-sn")
			
		},
		success : function(data) {
			$("#healthdialog").empty().html(data);
			deleteM(t,null);
		}
	});
}
//执行修改动作
function updateHeredity(){
	var memberId=$("#memberId").val();
	var health_heredity_id=$("#health_heredity_id").val();
	var diseaseName=$("#diseaseName").val();
	var lastUpdateNo=$("#lastUpdateNo").val();
	var remark=$("#heredityRemark").val();
	
	var hospitalName = $("#hospitalName").val();
	var confirmTime = $("#confirmTime").val();
	
	var sn=$("#updateSn").val();//序号
	var trId='healthHeredity_'+sn;
	if(!interceptIllegal(diseaseName)){
		zhyAlert("含有非法字符");
		return;
	}
	if(hospitalName){
		if($.trim(hospitalName).length > 32){
			zhyAlert("【诊断医院或社区】应少于32个字符！");
			return;
		}
		if(!interceptIllegal(hospitalName)){
			zhyAlert("【诊断医院或社区】含有非法字符");
			return;
		}
	}
	
	if(!validateDate(confirmTime)){
		zhyAlert("时间格式错误！");
		return;
	}

	if(remark){
		if($.trim(remark).length > 120){
			zhyAlert("【备注】应少于120个字符！");
			return;
		}
		if(!interceptIllegal(remark)){
			zhyAlert("【备注】含有非法字符");
			return;
		}
	}
	$.ajax({
		type : "post",
		dataType: "json",
		url : "../heredity/updateHealthHeredity",
		async: true,
		data:{
			healthHeredityId:health_heredity_id,
			diseaseName:diseaseName,
			lastUpdateNo:lastUpdateNo,
			remark:remark,
			confirmTime:confirmTime,
			hospitalName:hospitalName
		},
		success : function(data) {
			if(data.isSuccess){
				var heal_edit2 = data.heal_edit;//修改标示
				var heal_delete2 = data.heal_delete;//删除标示
				lastUpdateNo=eval(lastUpdateNo)+1;
				updateSuccessHeredity(trId,sn,diseaseName,health_heredity_id,lastUpdateNo,remark,heal_edit2,heal_delete2,hospitalName,confirmTime);
				hidePop();
				
			}else{
				zhyAlert("保存失败");
				hidePop();
			}
		}
	});
}
//删除
function removeHeredity(t){
	var iconFirm=window.confirm("确定删除这条记录？");
	var sn=$(t).attr("data-sn");
	var dataSize
	var trId;
	var health_heredity_id=$(t).attr("data-id");
	var lastUpdateNo=$(t).attr("data-lastUpdateNo");
	
	
	var sizeOfList=$("#sizeOfListHeredity").val();
	
	
	if(iconFirm==true){
		$.ajax({
			type : "post",
			dataType: "json",
			url : "../heredity/deleteHealthHeredity",
			async: true,
			data:{
				healthHeredityId:health_heredity_id,
				lastUpdateNo:lastUpdateNo
			},
			success : function(data) {
				if(data.isSuccess){
					sizeOfList=(eval(sizeOfList)-1);
					$("#sizeOfListHeredity").val(sizeOfList);
					removerTr(t);
					hidePop();
					
				}else{
					zhyAlert("删除失败");
					hidePop();
				}
			}
		});
		
	}
	
}

function removerTr(obj){
	
	obj =$(obj);
	var index =obj.parents("tr").find("td").eq(0);
	var next_tr=obj.parents("tr").nextAll();
	var trLenth=obj.parents("tbody").find("tr").length;
	if(trLenth==1){
		obj.parents("table").append("<tr id='NoDataNoHeredity'><td style='text-align: center' colspan='5'>暂无数据</td></tr>");
	}
	next_tr.each(function(){
		var td=$(this).find("td").eq(0);
		td.text(td.text()-1);
	})
	obj.parents("tr").remove();
}




//修改成功调用
function updateSuccessHeredity(trId,sn,diseaseName,health_heredity_id,lastUpdateNo,remark,heal_edit,heal_delete,hospitalName,confirmTime){
	var updateTr=$("#"+trId);
	var edit_str2 = "";
		edit_str2 ="<span class='colorblue edit'>  " +
						"<a  data-id='"+health_heredity_id+"' data-sn='"+sn+"' " +
						"data-lastUpdateNo='"+lastUpdateNo+"' onclick='updateHeredityPre(this)' " +
						"href='javascript:;' class='colorblue'>修改</a></span>";
	var del_str2 = "";
		del_str2= "<span data-id='"+health_heredity_id+"' data-lastUpdateNo='"+lastUpdateNo+"' " +
								"  class='colorred delete' onclick='removeHeredity(this)'>删除</span>";
	var newTr="<td>"+sn+"</td>"+
				"<td title='"+diseaseName+"'>"+diseaseName.substring(0,8)+(diseaseName.length>8?"...":"")+"</td>"+
				"<td>"+confirmTime+"</td>"+
				"<td >"+hospitalName+"</td>"+
				"<td title='"+remark+"'>"+remark.substring(0,16)+(remark.length>16?"...":"")+"</td>"+
				"<td>" + edit_str2 + del_str2 +"</td>";
	
	updateTr.empty();
	updateTr.append(newTr);
	
}
function addRowHeredity(sizeOfList,diseaseName,health_heredity_id,remark,heal_edit,heal_delete,hospitalName,confirmTime){
	var iTable;
	var trId;
	iTable=$("#hereidyTable");
	trId='healthHeredity_';
	$("#NoDataNoHeredity").remove();
	$("#sizeOfListHeredity").val(sizeOfList);
	var edit_str1 = "";
		edit_str1 ="<span class='colorblue edit'><a data-id='"+health_heredity_id+"' data-sn='"+sizeOfList+"' " +
			"data-lastUpdateNo='1' onclick='updateHeredityPre(this)' " +
			"href='javascript:;' class='colorblue'>修改</a> </span>";
	var del_str1 = "";
		del_str1 = "<span data-id='"+health_heredity_id+"' data-lastUpdateNo='1' " +
					"  class='colorred delete' onclick='removeHeredity(this)'>删除</span>";
	iTable.append("<tr id="+trId+sizeOfList+">" +
			"<td>"+sizeOfList+"</td>"+
			"<td title='"+diseaseName+"'>"+diseaseName.substring(0,8)+(diseaseName.length>8?"...":"")+"</td>"+
			"<td>"+confirmTime+"</td>"+
			"<td >"+hospitalName+"</td>"+
			"<td title='"+remark+"'>"+remark.substring(0,16)+(remark.length>16?"...":"")+"</td>"+
			"<td> "+ edit_str1 + del_str1 + "</td></tr>");
}



